﻿using AdminSystem.Applications;
using AdminSystem.Domain.User;
using AdminSystem.Repositories;
using AdminSystem.Write.API.Command.FarmsCommand;
using MediatR;

namespace AdminSystem.Write.API.Handers.FarmsHander
{
    public class FarmsQueryCommandsHandler : IRequestHandler<FarmsQueryCommand, ApiResult<Farms>>
    {
        public FarmsQueryCommandsHandler(IRepository<Farms> repository)
        {
            Repository = repository;
        }

        public IRepository<Farms> Repository { get; }
        /// <summary>
        /// 查询养殖场信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<Farms>> Handle(FarmsQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<Farms> result = new ApiResult<Farms>();
                result.statusCode = StatusCode.Success;
                result.Message = "查询成功";
                var list = Repository.GetAll();
                var entity = list.Result.FirstOrDefault();
                result.Data = entity;
                return Task.FromResult(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
